package com.techsoul.module.baoli.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.lang.DateUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.log.annotation.Log;
import com.techsoul.common.log.enums.OperationTypeEnum;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.baoli.enmus.FinCreditBillStatusEnum;
import com.techsoul.module.baoli.pojo.param.FinCreditBillSaveParam;
import com.techsoul.module.baoli.pojo.param.FinCreditBillSearchParam;
import com.techsoul.module.baoli.pojo.vo.FinCreditBillVo;
import com.techsoul.module.baoli.service.IFinCreditBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;

/**
 * 企业白条主 Controller
 *
 * @author liuen
 * @date 2022-03-28
 */
@Api(tags = "企业白条主")
@RestController
@RequestMapping("/baoli/finCreditBill")
public class FinCreditBillController {

    @Autowired
    private Result result;
    
    @Autowired
    private IFinCreditBillService service;
    @Autowired
    private TokenService tokenService;
    @ApiOperation("新增")
    @Log(title = "企业白条主", operationType = OperationTypeEnum.INSERT)
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody FinCreditBillSaveParam params) {
        return result.success(this.service.insert(params));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @Log(title = "企业白条主", operationType = OperationTypeEnum.DELETE)
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @Log(title = "企业白条主", operationType = OperationTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:update')")
    @PutMapping
    public Result update(@Valid @RequestBody FinCreditBillSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {

        FinCreditBillVo finCreditBillVo=this.service.findById(id);
        Double days = DateUtils.getDistanceOfTwoDate(new Date(),finCreditBillVo.getValidatedTo());
        finCreditBillVo.setUseageDays(days);

        return result.success(finCreditBillVo);
    }
    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:baolilist')")
    @PostMapping("/baoliSearch/{current}/{pageSize}")
    public Result baoliSearch(@RequestBody FinCreditBillSearchParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(params.getStatus()!=null&&params.getStatus()!=""){
            if(params.getStatus().equals(FinCreditBillStatusEnum.APPLYPAYMENT.getValue())){
                params.setBaoliReceiverSite("");
            }else{
                params.setBaoliReceiverSite(user.getSite());
            }
        }

        return result.success(this.service.findAll(params));
    }
    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:recelist')")
    @PostMapping("/receSearch/{current}/{pageSize}")
    public Result findRecell(@RequestBody FinCreditBillSearchParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

        params.setReceiverSite(user.getSite());
        return result.success(this.service.findAll(params));
    }
    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:search')")
    @PostMapping("/baoliPaymentSearch/{current}/{pageSize}")
    public Result baoliPaymentSearch(@RequestBody FinCreditBillSearchParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

        params.setBaoliPayableSite(user.getSite());
        return result.success(this.service.findAll(params));
    }
    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody FinCreditBillSearchParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        params.setSite(user.getSite());

        return result.success(this.service.findAll(params));
    }
    @ApiOperation("签收")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:rece')")
    @PostMapping("/receive")
    public Result receive(@RequestBody FinCreditBillSaveParam params) {

        String ret = service.receive(params);
        return result.success();

    }
    @ApiOperation("复核")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:prove')")
    @PostMapping("/prove")
    public Result prove(@RequestBody FinCreditBillSaveParam params) {

        String ret = service.prove(params);
        return result.success();

    }
    @ApiOperation("贴现确认")
  //  @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:paymentConfirm')")
    @PostMapping("/paymentConfirm")
    public Result paymentConfirm(@RequestBody FinCreditBillSaveParam params) {

        String ret = service.paymentConfirm(params);
        return result.success();

    }
    @ApiOperation("贴现拒绝")
    //  @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:paymentConfirm')")
    @PostMapping("/paymentReject")
    public Result paymentReject(@RequestBody FinCreditBillSaveParam params) {
        params.setStatus("reject");
        String ret = service.paymentConfirm(params);
        return result.success();

    }
    @ApiOperation("贴现申请")
    @PreAuthorize("hasAnyAuthority(' baoli:finCreditBill:cashOut')")
    @PostMapping("/cashOut")
    public Result cashOut(@RequestBody FinCreditBillSaveParam params) {

        String ret = service.cashOut(params);
        return result.success();

    }

    @ApiOperation("放款审核")
    //@PreAuthorize("hasAnyAuthority('baoli:finCreditBill:platFormprove')")
    @PostMapping("/cashOutProve")
    public Result cashOutProve(@RequestBody FinCreditBillSaveParam params) {

        String ret = service.cashOutProve(params);
        return result.success();

    }
    @ApiOperation("保贴审核")
    //@PreAuthorize("hasAnyAuthority('baoli:finCreditBill:platFormprove')")
    @PostMapping("/platFormprove")
    public Result platFormprove(@RequestBody FinCreditBillSaveParam params) {

        String ret = service.platFormprove(params);
        return result.success();

    }
    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody FinCreditBillSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {


        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        params.setSite(user.getSite());
        return result.success(this.service.findAll(params, current, pageSize));
    }
    @ApiOperation("当前待处理惠单数量")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:search')")
    @PostMapping("/findTotalMyBill")
    public Result findTotalMyBill() {
        UserVo sysuser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        return result.success(this.service.findTotalMyBill(sysuser));
    }

    @ApiOperation("当前待处理惠单数量")
    @PreAuthorize("hasAnyAuthority('baoli:finCreditBill:search')")
    @PostMapping("/findTotalMyRejectBill")
    public Result findTotalMyRejectBill() {
        UserVo sysuser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        return result.success(this.service.findTotalMyRejectBill(sysuser));
    }
}
